<!DOCTYPE html>
<html lang="en">
<meta charset="utf-8">
<title>Layui</title>
<meta name="renderer" content="webkit">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<link rel="stylesheet" href="layui/css/layui.css" media="all">
<!-- 注意：如果你直接复制所有代码到本地，上述css路径需要改成你本地的 -->
<!-- 引入jquery.js -->
<script src="layui/jquery.min.js"></script>
<body>
<table class="layui-hide" id="test" lay-filter="test"></table>



<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>


<script src="layui/layui.js" charset="utf-8"></script>
<!-- 注意：如果你直接复制所有代码到本地，上述 JS 路径需要改成你本地的 -->





<script>
    layui.use('table', function(){
        var table = layui.table;

        table.render({
            elem: '#test'
            ,url:'/orderUser/findByPage'
            ,toolbar: '#toolbarDemo' //开启头部工具栏，并为其绑定左侧模板
            ,defaultToolbar: ['filter', 'exports', 'print', { //自定义头部工具栏右侧图标。如无需自定义，去除该参数即可
                title: '提示'
                ,layEvent: 'LAYTABLE_TIPS'
                ,icon: 'layui-icon-tips'
            }]
            ,title: '订单数据表'
            ,page: { //支持传入 laypage 组件的所有参数（某些参数除外，如：jump/elem） - 详见文档
                limit:10,
                limits:[3,5,10],
                layout: ['limit', 'count', 'prev', 'page', 'next', 'skip'] //自定义分页布局
                //,curr: 5 //设定初始在第 5 页
                ,groups: 2 //只显示 2 个连续页码
                ,first: false //不显示首页
                ,last: false //不显示尾页
            }
            ,cols: [[
                {field:'orderId', title:'编号', width:200, fixed: 'left'}
                ,{field:'userName', title:'姓名', width:200, edit: 'text'}
                ,{field:'orderAddress', title:'发货地址', width:200, edit: 'text'}
                ,{field:'orderStatus', title:'状态', width:200, edit: 'text'}
                ,{fixed: 'right', title:'操作', toolbar: '#barDemo', width:150}
            ]]
            //,page: true
        });

        //头工具栏事件
        table.on('toolbar(test)', function(obj){
            var checkStatus = table.checkStatus(obj.config.id);
            switch(obj.event){
                case 'getCheckData':
                    var data = checkStatus.data;
                    //layer.alert(JSON.stringify(data));

                    //---------批量删除操作-------------
                    //声明一个变量，存储id
                    var ids = '';
                    //遍历
                    for(var i = 0; i < data.length; i++){
                        ids+="&ids="+data[i].id;
                    }
                    //alert(ids);
                    //使用ajax
                    $.get("orderUser/deleteAll", ids,
                        function(data){
                            if("success" == data){
                                window.location.href="http://localhost:8080/index.html";
                            }
                        });

                    //---------------------------------
                    break;
                case 'excel':
                    var data = checkStatus.data;
                    //layer.alert(JSON.stringify(data));

                    //声明一个变量，存储id
                    var ids = '';
                    //遍历
                    for(var i = 0; i < data.length; i++){
                        ids+="&ids="+data[i].id;
                    }


                //自定义头工具栏右侧图标 - 提示
                case 'LAYTABLE_TIPS':
                    layer.alert('这是工具栏右侧自定义的一个图标按钮');
                    break;
            };
        });

        //监听行工具事件
        table.on('tool(test)', function(obj){
            var data = obj.data;

            //console.log(obj)
            if(obj.event === 'del') {
                layer.confirm('真的删除行么', function (index) {
                    console.log(data)
                    obj.del();
                    layer.close(index);

                    //从数据库中删除
                    $.ajax({
                        type: "POST",
                        url: "orderUser/deleteId",
                        data: "id=" + data.orderId,
                        success: function (msg) {
                            //location.reload();//重新加载当前页面
                            table.reload("test", {});
                        }
                    });
                });

            } else if(obj.event === 'edit'){
                //layer.alert('编辑行：<br>'+ JSON.stringify(data))
                //首先调用的是查询单个的方法
                queryOne(data);//把数据渲染到修改页面
            }
        });
    });

</script>

<script>

    function queryOne(data) {
        layer.open({
            title:['修改信息'],
            type: 1,
            content: $("#UpdateInformation").html(), //修改的页面
            area:['600px','600px'],
            btn:['提交','取消'],

            //弹出后回调
            success:function () {
                $("#orderId").val(data.id);
                // $("#userName").val(data.userName);
                $("#orderAddress").val(data.orderAddress);
                $("#orderDate").val(data.orderDate);
                $("#orderStatus").val(data.orderStatus);

                //渲染页面
                layui.form.render();//渲染页面
            },
            //提交调用该函数
            yes:function () {
                $.ajax({
                    type: "POST",
                    url: "/orderUser/UpdateOrder",
                    data: $("#updateProductForm").serialize(),
                    success: function(msg){
                        location.reload();//重新加载当前页面
                    }
                });
            },
            //取消
            btn2:function () {
                alert("已取消")
            }
        });

    }

</script>

<script type="text/html" id="UpdateInformation">

    <form id="updateProductForm" class="layui-form">
        <!-- 页面上不显示id，但是需要作为隐藏域传到后台 -->
<!--        <input type="hidden" id="id" name="goodsId">-->

        <div class="layui-form-item">
            <div class="layui-input-block">
                <input id="orderId" type="hidden" name="orderId" lay-verify="title" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">总金额</label>
            <div class="layui-input-block">
                <input id="orderSumPrice" type="number" name="orderSumPrice" lay-verify="title" autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">订单人地址</label>
            <div class="layui-input-block">
                <input id="orderAddress" type="text" name="orderAddress" lay-verify="title" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">下单日期</label>
            <div class="layui-input-block">
                <input id="orderDate" type="date" name="orderDate" lay-verify="title" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">订单状态</label>
            <div class="layui-input-block">
                <input id="orderStatus" type="number" name="orderStatus" lay-verify="title" autocomplete="off" class="layui-input">
            </div>
        </div>
    </form>

</script>
</body>
</html>